package selenium;


import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;
import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.get("http://192.168.9.122/ordermaker/admin");
        Duration timeout = Duration.ofSeconds(30);
        Duration sleep = Duration.ofMillis(200);
        WebDriverWait wait = new WebDriverWait(driver,timeout,sleep);
        WebElement userNameInput = wait.until(new Function<WebDriver, WebElement>() {
            @Override
            public WebElement apply(WebDriver webDriver) {
                By locator = By.ByName.name("username");
                return webDriver.findElement(locator);
            }
        });
        userNameInput.sendKeys("admin");
        WebElement passwordInput = wait.until(new Function<WebDriver, WebElement>() {
            @Override
            public WebElement apply(WebDriver webDriver) {
                By locator = By.ByName.name("password");
                return webDriver.findElement(locator);
            }
        });
        passwordInput.sendKeys("admin");
        By locator = By.tagName("button");
        List<WebElement> elements = driver.findElements(locator);
        System.out.println(elements.size());
        for(WebElement element:elements){
            System.out.println(element.getText());
            if(element.getText().equals("登陆")){
                element.click();
                break;
            }
        }

//        driver.close();
    }
}
